# Copyright (c) 2021-2023, Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

project(mynewt-nimble LANGUAGES C)

include(FetchContent)

if("mynewt-nimble" IN_LIST IOTSDK_FETCH_LIST)
    FetchContent_MakeAvailable(mynewt-nimble)
endif()

if(mynewt-nimble_POPULATED)
    # TinyCrypt is required by NimBLE in ble_sm_alg.c
    add_library(mynewt-nimble-tinycrypt STATIC)

    target_sources(mynewt-nimble-tinycrypt
        PRIVATE
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/aes_decrypt.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/aes_encrypt.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/cbc_mode.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ccm_mode.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/cmac_mode.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ctr_mode.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ctr_prng.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ecc_dh.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ecc_dsa.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ecc_platform_specific.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/ecc.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/hmac_prng.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/hmac.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/sha256.c
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/src/utils.c
    )
    target_include_directories(mynewt-nimble-tinycrypt
        PUBLIC
            ${mynewt-nimble_SOURCE_DIR}/ext/tinycrypt/include
    )

    add_library(mynewt-nimble STATIC)

    target_sources(mynewt-nimble
        PRIVATE
            #nimble host core
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_att_clt.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_att_cmd.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_att_svr.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_att_svr.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_att.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_gap.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_gattc.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_gatts.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_adv.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_atomic.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_cfg.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_conn.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_flow.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_hci_cmd.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_hci_evt.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_hci_util.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_hci.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_id.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_log.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_mbuf.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_misc.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_mqueue.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_periodic_sync.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_pvcy.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_startup.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_shutdown.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs_stop.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_hs.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_l2cap_coc.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_l2cap_sig_cmd.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_l2cap_sig.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_l2cap.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_sm_alg.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_sm_cmd.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_sm_lgcy.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_sm_sc.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_sm.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_store.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_store_util.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/src/ble_uuid.c

            ${mynewt-nimble_SOURCE_DIR}/nimble/host/util/src/addr.c

            # temporary storage in ram
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/store/ram/src/ble_store_ram.c

            #transport
            ${mynewt-nimble_SOURCE_DIR}/nimble/transport/src/transport.c
            ${mynewt-nimble_SOURCE_DIR}/nimble/transport/common/hci_h4/src/hci_h4.c

            #services
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/services/gap/src/ble_svc_gap.c

            #service gatt
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/services/gatt/src/ble_svc_gatt.c

            #nimble porting
            ${mynewt-nimble_SOURCE_DIR}/porting/nimble/src/endian.c
            ${mynewt-nimble_SOURCE_DIR}/porting/nimble/src/mem.c
            ${mynewt-nimble_SOURCE_DIR}/porting/nimble/src/nimble_port.c
            ${mynewt-nimble_SOURCE_DIR}/porting/nimble/src/os_mbuf.c
            ${mynewt-nimble_SOURCE_DIR}/porting/nimble/src/os_mempool.c
            ${mynewt-nimble_SOURCE_DIR}/porting/nimble/src/os_msys_init.c
    )

    target_include_directories(mynewt-nimble
        PRIVATE
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/store/ram/include
        PUBLIC
            ${mynewt-nimble_SOURCE_DIR}/nimble/include

            ${mynewt-nimble_SOURCE_DIR}/nimble/transport/include
            ${mynewt-nimble_SOURCE_DIR}/nimble/transport/socket/include

            ${mynewt-nimble_SOURCE_DIR}/nimble/host/include
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/util/include

            #There are several more services to add if needed
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/services/gap/include
            ${mynewt-nimble_SOURCE_DIR}/nimble/host/services/gatt/include

            ${mynewt-nimble_SOURCE_DIR}/nimble/transport/common/hci_h4/include
    )

    target_compile_features(mynewt-nimble
        PRIVATE
            c_std_11 # minimal version required because of the use of static_asserts
    )
endif()
